<?php
/**
 * Custom News Feed model.
 * Author : MSE
 * Date   : 01 June 2010
 * Project: shopping.mu
 */
class ModelCatalogNewsFeed extends Model {
	
	/**
	 * Adds information.
	 */
	public function addNews($data) {
	
		// Create the vendor
      	$this->db->query("INSERT INTO " . DB_PREFIX . "news SET is_published = '" . (int)$data['is_published'] . "'");
		
		// Get vendor_id
		$news_id = $this->db->getLastId();
		
		// News items
		foreach ($data['news_item'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "news_item SET news_id = '" . (int)$news_id . "', language_id = '" . (int)$language_id . "', news_item = '" . $this->db->escape($value['news_item']) . "'");
		}
		
		// Author
		if (isset($data['author'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "news SET author = '" . $this->db->escape($data['author']) . "' WHERE news_id = '" . (int)$news_id . "'");
		}
		
		$this->cache->delete('newsfeed');
	}
	
	/**
	 * Edit news feed
	 */
	public function editNews($news_id, $data) {
	
		// Published status
      	$this->db->query("UPDATE " . DB_PREFIX . "news SET is_published = '" . (int)$data['is_published'] . "' WHERE news_id = '" . (int)$news_id . "'");

		// Author
		if (isset($data['author'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "news SET author = '" . $this->db->escape($data['author']) . "' WHERE news_id = '" . (int)$news_id . "'");
		}
		
		// Remove all related news items
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_item WHERE news_id = '" . (int)$news_id . "'");
		
		// News items
		foreach ($data['news_item'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "news_item SET news_id = '" . (int)$news_id . "', language_id = '" . (int)$language_id . "', news_item = '" . $this->db->escape($value['news_item']) . "'");
		}	
		
		$this->cache->delete('newsfeed');
	}
	
	/**
	 * Delete news items.
	 */
	public function deleteNews($news_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$news_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_item WHERE news_id = '" . (int)$news_id . "'");
	
		$this->cache->delete('newsfeed');
	}	
	
	/**
	 * Retrieve a news details.
	 */
	public function getNews($news_id) {
			
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$news_id . "'");
		
		return $query->row;
	}
	
	/**
	 * Get multiple news items
	 */
	public function getAllNews($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "news n LEFT JOIN " . DB_PREFIX . "news_item ni ON (n.news_id = ni.news_id) WHERE ni.language_id = '" . (int)$this->config->get('config_language_id') . "'";
			
			/*
			$sort_data = array(
				'name',
				'sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			*/
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}					

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}				
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$news_data = $this->cache->get('newsfeed');
		
			if (!$news_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news n LEFT JOIN " . DB_PREFIX . "news_item ni ON (n.information_id = ni.information_id) WHERE ni.language_id = '" . (int)$this->config->get('config_language_id'));
	
				$news_data = $query->rows;
			
				$this->cache->set('newsfeed', $news_data);
			}
		 
			return $news_data;
		}
	}

	/**
	 * Get news item associated with news.
	 */ 
	public function getNewsItem($news_id) {
	
		$news_item_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_item WHERE news_id = '" . (int)$news_id . "'");

		foreach ($query->rows as $result) {
			$news_item_data[$result['language_id']] = array(
				'news_item'       => $result['news_item']
			);
		}
		
		return $news_item_data;
	}
	
	/**
	 * Count total news pieces found (irrespective of language).
	 */
	public function getTotalNews() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "news");
		
		return $query->row['total'];
	}	
	
	/**
	 * Return the total number of news items associated to the id.
	 */
	public function getTotalNewsItemByNewsId($new_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "news_item WHERE news_id = '" . (int)$news_id . "'");
		
		return $query->row['total'];
	}
	
}
?>